package com.nerve.secondhandcar.dao;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.ko.core.dao.hibernate4.BaseTypedDao;
import org.ko.core.dao.support.Finder;
import org.ko.core.websupport.page.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.nerve.secondhandcar.domain.Brand;


/**
 * 平台中的汽车品牌DAO层
 * @author 集成显卡
 * @version 2014-10-09 17:26:50
 */
 @Repository
public class BrandDao  extends BaseTypedDao<Brand, Long>{

	@Override
	protected Class<Brand> getEntityClass() {
		return Brand.class;
	}
	
	public Page findPage(int pageNo, int pageSize,String searchText) {
		System.out.println("================================================");
		
		Finder f = Finder.create("FROM Brand where 1=1 ");
		if(StringUtils.isNotBlank(searchText)){
			f.append(" and (name like :searchText )");
			f.setParam("searchText", "%"+searchText+"%");
		}
		f.append(" order by firstLetter asc, sort desc");
		return find(f, pageNo, pageSize);
	}

	public List findTree(String searchText) {
		Finder f = Finder.create("");
		f.append("select  ")
		 .append(" id ,")
		 .append(" name ,")
		 .append(" firstLetter ,")
		 .append(" publicSelect ,")
		 .append(" pid ,")
		 .append(" sort ");
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		if(StringUtils.isNotBlank(searchText)){
			f.append(" and (name like :searchText )");
			f.setParam("searchText", "%"+searchText+"%");
		}
		f.append(" order by firstLetter asc, sort desc");
		return findBySql(f, Map.class);
	}
	
	/**
	 * 查找权限菜单(包含子菜单)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findAuthBrands() {
		Finder f = Finder.create("");
		f.append("select  ")
		 .append(" id ,")
		 .append(" name ,")
		 .append(" firstLetter ,")
		 .append(" publicSelect ,")
		 .append(" pid ,")
		 .append(" sort ");
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		f.append(" and pid is null ");
		f.append(" order by sort desc");
		
		List<Map> topMenus = findBySql(f, Map.class);
		for (Map tm : topMenus) {
			BigInteger pid = (BigInteger) tm.get("id");
			tm.put("sublist", findByParent(pid==null?null:pid.longValue()));
		}
		return topMenus;
	}
	/**
	 * 根据父ID查的子菜单
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findByParent(Long pid) {
		Finder f = Finder.create("");
		f.append("select  ")
		 .append(" id ,")
		 .append(" name ,")
		 .append(" firstLetter ,")
		 .append(" publicSelect ,")
		 .append(" pid ,")
		 .append(" sort ");
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		f.append(" and pid=:pid").setParam("pid", pid);
		f.append(" order by sort desc");
		
		return findBySql(f, Map.class);
	}

	public int changeParent(Long id, Long pid) {
		Assert.notNull(id,"ID不能为空");
		Assert.notNull(pid,"父ID不能为空");
		return updateBySql("update "+getTableName()+" set pid=? where id=?",pid, id);
	}
	
	
	/**
	 * 获取显示在首页的品牌
	 * @method name: brandOnIndex
	 * @return type: List<Brand>
	 *	@return
	 */
	public List<Brand> brandOnIndex(){
		Finder f = Finder.create("FROM Brand where parent = null and publicSelect=1 order by sort desc");
		return find(f);
	}
}